<?php
// +----------------------------------------------------------------------
// | 青航系统 [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2023-永久 https://www.feifeiyu.club/ All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 青航-不吃芒果 <1716892803@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\exception\Jump;
use think\facade\View;
use app\admin\validate\Category_validate;
use app\admin\model\Category as CategoryModel;
class Category extends  Common{
    
    
    
    // 数据列表
    public function index() {
        $list = D('category')->order('listorder ASC')->select();
        $cate = tree_cate($list);
    	return view('category/index',['list'=>$cate]);
    }
    
    
    
    
    
    // 添加数据
    public function add() {
    	$type = request()->param('type');
    	// 	输出数据集合
    	$cate = D('category')->order('listorder ASC')->select();
    	$cate = tree_cate($cate);
        // 	渲染出模板
    	View::assign(['cate'=>$cate,'template'=>getTemplate(),'type'=>$type]);
    	if (request()->isPost()) {
    	    // 获取全部参数
    	    $data = input('post.');
    	     // 加载验证器
    	    $validate = new Category_validate();
    	    
            if (!$validate->check($data)) {
               Jump::error($validate->getError());
            }
            
            try{
			$res = CategoryModel::insertGetId($data);
    		}catch(\Exception $e){
    			throw new \think\exception\ValidateException($e->getMessage());
    		}
    		
            $categoryId = D('category')->getLastInsID();
            
    		if($data['type']=='1')$href = '/'.'list'.'/'.$categoryId.'.html';
    		if($data['type']=='2')$href = '/'.'list'.'/'.$categoryId.'.html';
    		if($data['type']=='3')$href = $data['pclink'];
    		D('category')->where('id',$categoryId)->update(['pclink'=>$href]);
    		Jump::success('添加成功');
    	}
    	sleep(1);
    	return view('category/category_add');
    }
    
    
    
    
    
    
    // 更新数据
    public function edit() {
    	$id = request()->param('id');
        //  查找对应的值
    	$find = D('category')->where('id',$id)->find();
    	// 	输出数据集合
    	$cate = D('category')->order('listorder ASC')->select();
    	$cate = tree_cate($cate);
         // 	渲染模板数据
    	View::assign(['cate'=>$cate,'template'=>getTemplate(),'find'=>$find]);
    	if (request()->isPost()) {
    	   // 获取全部参数
    	    $data = input('post.');
    	     // 加载验证器
    	    $validate = new Category_validate();
    	    
            if (!$validate->check($data)) {
               Jump::error($validate->getError());
            }
            
            try{
			$res = CategoryModel::update($data);
    		}catch(\Exception $e){
    			throw new \think\exception\ValidateException($e->getMessage());
    		}
    		
            Jump::success('更新成功');
    	}
    	sleep(1);
    	return view('category/category_edit');
    }
    
    
    
    // 删除数据
    public function del() {
    	if (request()->isPost()) {
    	   // 获取当前网页的参数ID
    		$id = request()->param('id');
    		if(empty($id) ) {Jump::error('ID不存在!');}
            // 查询负极ID是否存在
            $find = D('category')->where('parentid',$id)->find();
            if($find)Jump::error('该分类有下级分类!');
    		if(D('category')->where('id',$id)->delete()){
    		  Jump::success("删除成功!!");     
    		}
    		else{
    		  Jump::error('删除失败!');
    		}  
    	}
    }
    
 
    
}